<?php

namespace App\Api\Services;

use App\Api\Models\SupplierCustomerModel;

class SupplierCustomerService
{
    //检查客户信息
    static public function checkInfo($data)
    {
        $result = SupplierCustomerModel::checkInfo($data);

        if(empty($result)) {
            return true;
        }
        else {
            return false;
        }
    }

    //列表
    static public function index($data)
    {
        $keyword = isset($data['keyword'])?$data['keyword']:'';
        return SupplierCustomerModel::index(
            $keyword, $data
        );
    }

    //编辑
    static public function edit($data)
    {
        //搜索要修改的目标数据
        $searchTargetInfo = SupplierCustomerModel::where([
            'id' => $data['id'],
            'user_id' => $data['user_id']
        ])->first();

        $searchSameInfo = SupplierCustomerModel::where([
            'compony_name' => empty($searchTargetInfo['compony_name'])?'':$searchTargetInfo['compony_name'],
            'name' => empty($searchTargetInfo['name'])?'':$searchTargetInfo['name'],
            'phone' => empty($searchTargetInfo['phone'])?'':$searchTargetInfo['phone'],
            'address' => empty($searchTargetInfo['address'])?'':$searchTargetInfo['address'],
            'user_id' => $data['user_id']
        ])->get()->toArray();

        //批量更新
        foreach($searchSameInfo as $value) {
            SupplierCustomerModel::where('id', $value['id'])
                ->update([
                    'compony_name' => empty($data['compony_name'])?'':$data['compony_name'],
                    'name' => empty($data['name'])?'':$data['name'],
                    'phone' => empty($data['phone'])?'':$data['phone'],
                    'address' => empty($data['address'])?'':$data['address'],
                ]);
        }

        SupplierCustomerModel::where('id', $data['id'])->update($data);

        return true;
    }
}
